import pytest, os
from core.apiserver import ApiServer
from utils.readutil import Readdata
from utils.yamlutil import YamlUtil
from core.apiclient import ApiClient


# success_login_data1 = YamlUtil().assemble_yaml("login","success_login.yaml","success_login_new")


@pytest.fixture()
def login():
    if "token" not in os.environ:
        success_login_path = os.path.join(os.path.dirname(os.path.dirname(os.path.realpath(__file__))), "data",
                                          "success_login.yaml")

        success_login_data = Readdata().read_yaml(success_login_path)["success_login_new"][0]

        url = success_login_data["request_info"]["url"]
        method = success_login_data["request_info"]["method"]
        json_data = success_login_data["case_info"][0]["json"]
        res_json = ApiClient().do_request(url=url, method=method, json=json_data)
        os.environ["token"] = res_json["token"]
        headers = {
            "Authorization": "JWT " + res_json['token']
        }
        return res_json["token"], headers
    else:
        headers = {
            "Authorization": "JWT " + os.environ['token']
        }
        return os.environ["token"],headers
    # ApiServer().info(success_login_data1)
